import { Module } from 'vuex'
import { IHomeState } from './type'
import { IRootState } from '../types'
import {
  getUerInfo,
  getLikeSong,
  getUserFollows,
  getUserFolloweds,
  getUserEvent
} from '@/service/user/home/home'

const homeModule: Module<IHomeState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userInfo: {
        id: 0,
        userName: '',
        avatarUrl: '',
        nickname: '',
        birthday: 0,
        signature: '',
        city: 0,
        vipType: 0
      },
      userFans: {
        followeds: null,
        follows: null,
        UserEvent: null
      },
      codeImg: '',
      codeKey: '',
      codeMessage: 0,
      songHistoryWeek: [],
      songHistoryAll: [],
      weekSingsCount: 0,
      allSingsCount: 0
    }
  },
  mutations: {
    changeHomeState(state, loginData) {
      state.userInfo.avatarUrl = loginData.profile.avatarUrl
      state.userInfo.birthday = loginData.profile.birthday
      state.userInfo.id = loginData.account.id
      state.userInfo.nickname = loginData.profile.nickname
      state.userInfo.signature = loginData.profile.signature
      state.userInfo.userName = loginData.account.userName
      state.userInfo.city = loginData.profile.city
      state.userInfo.vipType = loginData.profile.vipType
    },
    changeHomeLikeSong(state, songHistory) {
      state.songHistoryWeek = songHistory[0].songHistoryWeek
      state.songHistoryAll = songHistory[1].songHistoryAll
    },
    // changeHomeTimes(state, allTimes) {
    //   state.songHistoryWeek = songHistory[0].songHistoryWeek
    //   state.songHistoryAll = songHistory[1].songHistoryAll
    // },
    changeHomeFans(state, userFans) {
      state.userFans.followeds = userFans[1].UserFolloweds
      state.userFans.follows = userFans[0].follows
      state.userFans.UserEvent = userFans[2].UserEvent
    }
  },
  actions: {
    async getHomeUserInfo({ commit }, userID: number) {
      const HomeResult = await getUerInfo(userID)
      const { allData: homeLikeSongResAll } = await getLikeSong(userID)
      const songHistoryAll: { playCount: any; songName: any; singer: any; singID: any }[] = []
      homeLikeSongResAll.slice(0, 10).map((item: any) => {
        return songHistoryAll.push({
          playCount: item.playCount,
          songName: item.song.name,
          singer: item.song.ar[0].name,
          singID: item.song.id
        })
      })
      const { allData: homeLikeSongResWeek } = await getLikeSong(userID)
      const songHistoryWeek: { playCount: any; songName: any; singer: any; singID: any }[] = []
      homeLikeSongResWeek.slice(0, 10).map((item: any) => {
        return songHistoryWeek.push({
          playCount: item.playCount,
          songName: item.song.name,
          singer: item.song.ar[0].name,
          singID: item.song.id
        })
      })
      const songHistory = []
      songHistory.push({ songHistoryWeek }, { songHistoryAll })

      commit('changeHomeLikeSong', songHistory)
      commit('changeHomeState', HomeResult)
    },
    async getHomeFans({ commit }, userID: number) {
      const { follow: follows } = await getUserFollows(userID)
      const { followeds: UserFolloweds } = await getUserFolloweds(userID)
      const { events: UserEvent } = await getUserEvent(userID)
      const userFans = []
      userFans.push({ follows }, { UserFolloweds }, { UserEvent })
      commit('changeHomeFans', userFans)
    }
  },
  getters: {}
}
//
export default homeModule
